package mxm.amfire;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.soundcloud.android.crop.Crop;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import dmax.dialog.SpotsDialog;
import me.yokeyword.fragmentation.SupportFragment;
import okhttp3.Call;

/**
 * Created by zengpeijin on 2017/2/14.
 */

public class ZhiYunFragment extends SupportFragment implements View.OnClickListener {
    private View rootView;
    private RecyclerView mRecyclerView;
    private ImageView imageView;
    private Button btn1, btn2, btn3;
    private Integer res[] = {R.drawable.logo_demo1, R.drawable.logo_demo2, R.drawable.logo_demo3};
    private int index;
    public static ZhiYunFragment newInstance() {
        ZhiYunFragment fragment = new ZhiYunFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_zhiyun, container, false);
        initView();
        return rootView;
    }

    private void initView() {
        btn1 = (Button) rootView.findViewById(R.id.button);
        btn2 = (Button) rootView.findViewById(R.id.button2);
        btn3 = (Button) rootView.findViewById(R.id.button3);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        imageView = (ImageView) rootView.findViewById(R.id.imageView);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(_mActivity, LinearLayoutManager.HORIZONTAL, false));
        mRecyclerView.setAdapter(new RecyclerView.Adapter<MyViewHolder>() {
            @Override
            public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(_mActivity).inflate(R.layout.recycer_item_zhiyun, null, false));
                return myViewHolder;
            }

            @Override
            public void onBindViewHolder(MyViewHolder holder, int position) {
                holder.iv.setImageResource(res[position]);
                holder.itemView.setTag(position);
                holder.itemView.setOnClickListener(ZhiYunFragment.this);
            }

            @Override
            public int getItemCount() {
                return res.length;
            }

        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        System.out.println("----------------------------------------------------------------------");
    }

    @Override
    public void onClick(View view) {
        if (view.getTag() != null) {
            index=Integer.valueOf(view.getTag().toString());
            imageView.setImageResource(res[index]);
        } else if (view == btn1) {
                get();
        } else if (view == btn2) {
            Crop.pickImage(_mActivity);
        } else if (view == btn3) {

        }
    }
    public File saveBitmapFile(Bitmap bitmap){
        File file=new File(_mActivity.getCacheDir(),"temp.jpg");//将要保存图片的路径

        file.getParentFile().mkdirs();
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
    private void get() {
        final SpotsDialog dialog= new SpotsDialog(_mActivity, R.style.spot_dialog_loading);
        dialog.show();
        Map<String, String> params = new HashMap<>();
        params.put("api_key", "ffa6c808999c4199964fd223d3fff47e");
        params.put("api_secret", "fbc720c1bafe484faeabf83332c6b29b");
        Bitmap bmp=BitmapFactory.decodeResource(getResources(),res[index]);
        File file= saveBitmapFile(bmp);
        if(file.length()>3000000){
            return;
        }
        bmp.recycle();
        bmp=null;
        OkHttpUtils.post().addFile("img","01.jpg",file).params(params).url("https://www.intelvision.cn/api/plate/detection/detect").build().connTimeOut(20000).writeTimeOut(20000).readTimeOut(20000).execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                e.printStackTrace();
                System.out.println("错误");
                dialog.dismiss();
            }

            @Override
            public void onResponse(String response, int id) {
                dialog.dismiss();
                System.out.println(response);
                if (TextUtils.isEmpty(response)) {
                    return;
                }
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String color = jsonObject.optString("color", "无法识别颜色");
                    String logo = jsonObject.optString("logo", "无法识别车标");

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView iv;

        public MyViewHolder(View view) {
            super(view);
            iv = (ImageView) view.findViewById(R.id.imageView);
        }
    }
}
